//package com.yunhe.device.repository.device.impl;
//
//import com.yunhe.common.util.StringUtil;
//import org.neo4j.ogm.model.Result;
//import org.neo4j.ogm.session.Session;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Repository;
//
//import java.util.*;
//
//@Repository
//public class AnalogDao {
//
//    @Autowired
//    private Session session;
//
////    public List<Analog> findAnalogsByFirmStationAndType(Optional<Long> firmId,
////                                                        Optional<Long> stationId,
////                                                        Optional<Long> companyId,
////                                                        Long typeCode,
////                                                        Optional<String> terminal) {
////        Map<String, Object> map = new HashMap<>();
////        StringBuffer cql = new StringBuffer();
////        if (companyId.isPresent()) {
////            cql.append("start c=node({companyId}) match (c)");
////            map.put("companyId", companyId.get());
////        } else if (stationId.isPresent()) {
////            cql.append("start s=node({stationId}) match (s)-[:HAS_RESOURCE{type:'Company'}]->(c:Company)");
////            map.put("stationId", stationId.get());
////        } else if (firmId.isPresent()) {
////            cql.append("start f=node({firmId}) match (f:Firm)-[:HAS_STATION]->(s:Station)-[:HAS_RESOURCE{type:'Company'}]->(c:Company)");
////            map.put("firmId", firmId.get());
////        }
////        cql.append("-[:HAS_OFFICE]->(o:Office)-[:HAS_ANALOG");
////        if (terminal.isPresent()) {
////            cql.append("{terminal:{terminal}}");
////            map.put("terminal", terminal.get());
////        }
////        cql.append("]->(a:Analog)-[:POINTDATATYPE_OF]->(pdt:PointDataType{code:{typeCode}}) return a");
////        map.put("typeCode", typeCode);
////        return process(cql.toString(), map);
////    }
////
////    private List<Analog> process(String cql, Map<String, Object> map) {
////        List<Analog> result = new ArrayList<>();
////        Result query = session.query(cql, map);
////        Iterator<Map<String, Object>> iterator = query.iterator();
////        while (iterator.hasNext()) {
////            Map<String, Object> next = iterator.next();
////            Analog analog = (Analog) next.get("a");
////            result.add(analog);
////        }
////        return result;
////    }
//
//    public Map<Long, List<Long>> findCompanyPointNumber(Long firmId,
//                                                        Long stationId,
//                                                        Long companyId,
//                                                        Long typeCode,
//                                                        String terminal,
//                                                        String meter) {
//        Map<String, Object> map = new HashMap<>();
//        StringBuilder cql = new StringBuilder();
//        if (companyId != null) {
//            cql.append("start c=node({companyId}) match (c)");
//            map.put("companyId", companyId);
//        } else if (stationId != null) {
//            cql.append("start s=node({stationId}) match (s)-[:HAS_RESOURCE{type:'Company'}]->(c:Company{activity:true})");
//            map.put("stationId", stationId);
//        } else if (firmId != null) {
//            cql.append("start f=node({firmId}) match (f:Firm)-[:HAS_STATION]->(s:Station)-[:HAS_RESOURCE{type:'Company'}]->(c:Company{activity:true})");
//            map.put("firmId", firmId);
//        }
//        cql.append("-[:HAS_OFFICE]->(o:Office)-[:HAS_ANALOG");
//        if (!StringUtil.isEmpty(terminal)) {
//            cql.append("{terminal:{terminal}}");
//            map.put("terminal", terminal);
//        }
//        cql.append("]->(a:Analog)-[:POINTDATATYPE_OF]->(pdt:PointDataType{code:{typeCode}}) return id(c) as deviceId,toInt(a.pointNumber) as pointNumber");
//        map.put("typeCode", typeCode);
//        return getDevicePointNumber(cql.toString(), map);
//    }
//
//    public Map<Long, List<Long>> findStationPointNumber(Long firmId,
//                                                        Long stationId,
//                                                        Long typeCode,
//                                                        String terminal,
//                                                        String meter) {
//        Map<String, Object> map = new HashMap<>();
//        StringBuilder cql = new StringBuilder();
//        if (stationId != null) {
//            cql.append("start s=node({stationId}) match (s)");
//            map.put("stationId", stationId);
//        } else if (firmId != null) {
//            cql.append("start f=node({firmId}) match (f:Firm)-[:HAS_STATION]->(s:Station)");
//            map.put("firmId", firmId);
//        }
//        cql.append("-[:HAS_ANALOG");
//        if (!StringUtil.isEmpty(terminal)) {
//            cql.append("{terminal:{terminal}}");
//            map.put("terminal", terminal);
//        }
//        cql.append("]->(a:Analog)-[:POINTDATATYPE_OF]->(pdt:PointDataType{code:{typeCode}}) return id(s) as deviceId,toInt(a.pointNumber) as pointNumber");
//        map.put("typeCode", typeCode);
//        return getDevicePointNumber(cql.toString(), map);
//    }
//
//    public List<String> getMeterList(Long companyId,
//                                    String terminal) {
//        Map<String, Object> map = new HashMap<>();
//        StringBuilder cql = new StringBuilder();
//        cql.append("start c=node({companyId}) match (c)-[:HAS_OFFICE]->(o:Office)-[r:HAS_ANALOG");
//        map.put("companyId", companyId);
//        if (!StringUtil.isEmpty(terminal)) {
//            cql.append("{terminal:{terminal}}");
//            map.put("terminal", terminal);
//        }
//        cql.append("]->(a:Analog) return distinct r.meter as meter");
//        Result query = session.query(cql.toString(), map);
//        List<String> meterList = new ArrayList<>();
//        for (Map<String, Object> next : query) {
//            String meter = (String) next.get("meter");
//            meterList.add(meter);
//        }
//        return meterList;
//    }
//
//    private Map<Long, List<Long>> getDevicePointNumber(String cql, Map<String, Object> map) {
//        Map<Long, List<Long>> devicePointNumber = new HashMap<>();
//        Result query = session.query(cql, map);
//        for (Map<String, Object> next : query) {
//            Long deviceId = (Long) next.get("deviceId");
//            Long pointNumber = (Long) next.get("pointNumber");
//            List<Long> myPointNumber = devicePointNumber.computeIfAbsent(deviceId, k -> new ArrayList<>());
//            myPointNumber.add(pointNumber);
//        }
//        return devicePointNumber;
//    }
//}
